﻿using System;
using System.Collections.Generic;
using Rerun.Domain;
using Rerun.Exceptions;

namespace Rerun.Services {

    /// <summary>
    /// 
    /// </summary>
    public class ConfigurationService {
        private static int interval = -1;

        private static List<ProcessInfo> processInfoList;

        /// <summary>
        /// 
        /// </summary>
        public static int Interval {
            get {
                return interval * 1000;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static List<ProcessInfo> ProcessInfoList {
            get {
                return processInfoList;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string Application {
            get {
                return Properties.Settings.Default.Application;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static int ApplicationRestartWarningThreshhold {
            get {
                return Properties.Settings.Default.ApplicationRestartWarningThreshhold;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static bool ShowUserNotifications {
            get {
                return Properties.Settings.Default.ShowUserNotifications;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <exception cref="ConfigurationException"></exception>
        static ConfigurationService() {
            try {
                processInfoList = new List<ProcessInfo>(Properties.Settings.Default.ExecutableList.Count);
                foreach(string executableName in Properties.Settings.Default.ExecutableList) {
                    processInfoList.Add(new ProcessInfo(executableName));
                }

                interval = Properties.Settings.Default.Interval;
            } catch(Exception e) {
                throw new ConfigurationException(e.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private ConfigurationService() {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <exception cref="ConfigurationException"></exception>
        public static void ResetJustStartedValues() {
            try {
                foreach(var processInfo in processInfoList) {
                    processInfo.JustStarted = false;
                }
            } catch(Exception e) {
                throw new ConfigurationException(e.Message);
            }
        }
    }
}